package com.vkmessenger.gui.components;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.vkmessenger.R;
import com.vkmessenger.net.json.FriendsResponse;

/**
 * Created by IntelliJ IDEA.
 * User: Evgeniy Krasko
 * Date: 21.06.12
 * Time: 17:54
 */
public class ContactShortInfo extends LinearLayout {
    private final TextView nameTextView;
    private final ImageView photo;
    private final ImageView online;
    private final FriendsResponse.FriendResponse friend;

    public ContactShortInfo(Context context, FriendsResponse.FriendResponse friend) {
        this(context, null, friend);
    }

    public ContactShortInfo(Context context, AttributeSet attrs, FriendsResponse.FriendResponse friend) {
        super(context, attrs);
        this.friend = friend;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.single_contact, this);

        nameTextView = (TextView) findViewById(R.id.personName);
        online = (ImageView) findViewById(R.id.isOnline);
        photo = (ImageView) findViewById(R.id.contact_photo);

        update();
    }

    public void update() {
        online.setVisibility(friend.online.equals("1") ? VISIBLE : INVISIBLE);
        nameTextView.setText(friend.first_name + " " + friend.last_name);
    }

    public Character getFirstNameSymbol() {
        if (friend.first_name != null && friend.first_name.length() != 0) return friend.first_name.charAt(0);
        if (friend.last_name != null && friend.last_name.length() != 0) return friend.last_name.charAt(0);
        return null;
    }
}
